.SUFFIX: .a .axf .map .bin .scat .via .cfg .o .objs .txt



#
# Project and ARM settings
#

### The Name of Project
PRJ_NAME        := boot_strap
export PRJ_NAME

### ARM core (used by option)
ARMCORE         := cortex-a7
export ARMCORE

### Tool chain
TCHAIN  := $(GNUarm)

### CPU STATE
IS_ARM_STATE    ?=
export IS_ARM_STATE

### C source -> ASM
IS_GET_ASM      ?=
export IS_GET_ASM

### Feedback
IS_USE_FEEDBACK ?=
export IS_USE_FEEDBACK

##### DELETE_PGRELIEF #####
### PGRelief
IS_USE_PGRELIEF ?=
export IS_USE_PGRELIEF
##### DELETE_PGRELIEF #####

#
# Directory and File names
#

### Root Directory of the Project
ROOT_DIR        := ..

### The Name of Directory
OBJ_DIR_NAME    := obj
BIN_DIR_NAME    := bin
LIB_DIR_NAME    := lib
WS_DIR_NAME     := project
export WS_DIR_NAME

### The List of Sub-Directory which have Makefile
SUBDIRS         := $(ROOT_DIR)/boot
SUBDIRS         += $(ROOT_DIR)/MILB_Header
SUBDIRS         += $(ROOT_DIR)/NF
SUBDIRS         += $(ROOT_DIR)/emmc
### DELETE_TO_NO_WARP
SUBDIRS         += $(ROOT_DIR)/warp
### DELETE_TO_NO_WARP

### The List of Library
BASEFW_LIBS     :=
PERIPH_LIBS     :=
OS_LIBS         :=
FJ_LIBS         :=
IP_LIBS         :=

### Libraries
LIBS             = $(BASEFW_LIBS) $(PERIPH_LIBS) $(OS_LIBS) $(FJ_LIBS) $(IP_LIBS)

### Objects
OBJS             = $(patsubst %.o, $(ROOT_DIR)/%.o, $(shell cat $(ROOT_DIR)/project/$(OBJ_DIR_NAME)/*.objs 2>/dev/null))

### File (related to armlink)
### note : If you want to make library file, Variable "ELF_FILE" must be empty.
ELF_FILE        := $(BIN_DIR_NAME)/$(PRJ_NAME).axf
LIST_FILE       := $(BIN_DIR_NAME)/$(PRJ_NAME).map
SCAT_FILE       := $(PRJ_NAME).ld
LD_VIA_FILE     := gcclink.via
FEEDBACK_FILE   := armlink_feedback.txt
export FEEDBACK_FILE
FEEDBACK_BATCH  := feedback_full_build.bat

### File (related to armar)
ifeq "$(IS_MAKE_LIB)" "y"
LIB_FILE        := $(LIB_DIR_NAME)/$(PRJ_NAME).a
LIB_INFO_FILE   := $(LIB_DIR_NAME)/$(PRJ_NAME).txt
endif

### File (related to fromelf)
CVT_FILE        := $(BIN_DIR_NAME)/$(PRJ_NAME).bin

#
# Toolchain
#

### Linker
-include $gcclink.via
LD       := $(TCHAIN)gcc
LDFLAGS  := -march=armv7-a
LDFLAGS  += -mfpu=neon-vfpv4
LDFLAGS  += -mfloat-abi=softfp
LDFLAGS  += -DENABLE_NEON
LDFLAGS  += -mthumb-interwork
LDFLAGS  += -nostdlib
LDFLAGS  += -static
LDFLAGS  += -T $(SCAT_FILE)
LDFLAGS  += -Wl,-Map,$(LIST_FILE)

### Librarian
AR       := $(TCHAIN)ar
ARFLAGS  := -r

### Conveting Tool
CV       := $(TCHAIN)objcopy
CVFLAGS  := -Obinary



#
# Targets
#

ifeq "$(ELF_FILE)" ""
DEFAULT_TARGET := lib
else
DEFAULT_TARGET := target
endif

QUIET := @

.PHONY: default all recursive-target target-self lib lib-self convert distclean-self depends-self $(SUBDIRS) \
        target clean clean-self clean-obj clean-obj-self clean-elf clean-feedback-info distclean \
        depends message message-self feedback-all

default: $(DEFAULT_TARGET)

all: clean depends $(DEFAULT_TARGET) convert

recursive-target: $(PRE_TARGET) $(SUBDIRS) $(POST_TARGET)

target-self: $(CVT_FILE) lib-self

lib: depends target lib-self

lib-self: $(LIB_FILE)

convert: $(CVT_FILE)

distclean-self: clean-self

depends-self: 

### Sub Folders
ifneq "$(SUBDIRS)" ""
$(SUBDIRS): 
	$(QUIET)$(MAKE) recursive-target -C $@
endif

### Build
target: 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=target-self

$(LIB_FILE): $(OBJS)
	$(QUIET)if [ ! -d $(LIB_DIR_NAME) ] ; then mkdir $(LIB_DIR_NAME); fi
	$(QUIET)echo Creating $@ ...
	$(QUIET)$(AR) $(ARFLAGS) $@ $(OBJS)
	$(QUIET)$(AR) -t $@ > $(LIB_INFO_FILE)
	$(QUIET)echo done.

$(ELF_FILE): $(OBJS) $(LIBS) $(SCAT_FILE) $(LD_VIA_FILE)
	$(QUIET)if [ ! -d $(BIN_DIR_NAME) ] ; then mkdir $(BIN_DIR_NAME); fi
	$(QUIET)echo Creating $@ ...
	$(QUIET)$(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@ -lm -lc -lgcc
	$(QUIET)echo done.

$(CVT_FILE): $(ELF_FILE)
	$(QUIET)echo Converting to $@ from $< ...
	$(QUIET)if [ ! -d $(BIN_DIR_NAME) ] ; then mkdir $(BIN_DIR_NAME); fi
	$(QUIET)$(CV) $(CVFLAGS) $< $@
	$(QUIET)echo done.

### Clean
clean: 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=clean-self

clean-self: clean-obj-self
	$(QUIET)if [ -d $(BIN_DIR_NAME) ] ; then rm -rf $(BIN_DIR_NAME); fi
	$(QUIET)if [ -d $(LIB_DIR_NAME) ] ; then rm -rf $(LIB_DIR_NAME); fi

clean-obj: 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=clean-obj-self

clean-obj-self: 
	$(QUIET)if [ -d $(ROOT_DIR)/$(OBJ_DIR_NAME) ] ; then rm -rf $(ROOT_DIR)/$(OBJ_DIR_NAME); fi
	$(QUIET)if [ -d $(OBJ_DIR_NAME) ] ; then rm -rf $(OBJ_DIR_NAME); fi

clean-elf: 
	$(QUIET)if [ -f $(ELF_FILE) ] ; then rm -f $(ELF_FILE); fi

clean-feedback-info: 
	$(QUIET)if [ -f $(FEEDBACK_FILE) ] ; then rm -f $(FEEDBACK_FILE); fi

distclean: 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=distclean-self

### Depends
depends: 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=depends-self

### Message
message: 
	$(QUIET)$(MAKE) recursive-target PRE_TARGET=message-self

message-self: 
ifneq "$(MESSAGE)" ""
	$(QUIET)echo $(MESSAGE)
endif

### Feedback
feedback-all: 
	$(QUIET)$(FEEDBACK_BATCH)
